package com.xianshijian.oauth.userinfo.service;

import java.text.SimpleDateFormat;
import java.util.Date;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.stereotype.Service;

import com.xianshijian.oauth.common.entity.OAuth;
import com.xianshijian.oauth.common.entity.OAuthResult;
import com.xianshijian.oauth.userinfo.cache.UserCacheKey;
import com.xianshijian.oauth.userinfo.dao.ClientUserDao;
import com.xianshijian.oauth.userinfo.dao.ResumeDao;
import com.xianshijian.oauth.userinfo.dao.UserDao;
import com.xianshijian.oauth.userinfo.entity.ClientUser;
import com.xianshijian.oauth.userinfo.entity.ResultUser;
import com.xianshijian.oauth.userinfo.entity.Resume;
import com.xianshijian.oauth.userinfo.entity.User;

/**
 * 用户Service
 * 
 * @author lzj
 *
 */
@Service
public class UserInfoService {
	
	 @Autowired
	 private UserDao userDao;
	 @Autowired
	 private ClientUserDao clientUserDao;
	 @Autowired
	 private ResumeDao resumeDao;
    
    /**
     * 根据accessToken查找User
     * @param accessToken
     * @return
     */
//	@Cacheable(value = UserCacheKey.OAUTH_USER_BEAN_CACHE, key = "#p0")
    public OAuthResult findUserByAccessToken(String accessToken) {
		ClientUser clientUser = clientUserDao.findByAccessToken(accessToken);
		
		if(clientUser==null){
			return OAuthResult.build(OAuth.StatusCode.NOT_FOUND,"无此用户");
		}
		
		User user = userDao.findById(clientUser.getAccountId());
		if(user==null){
			return OAuthResult.build(OAuth.StatusCode.NOT_FOUND,"无此用户");
		}
		
		Resume resume = resumeDao.findByAccountId(clientUser.getAccountId());
		if(resume==null){
			return OAuthResult.build(OAuth.StatusCode.NOT_FOUND,"无此用户");
		}
		ResultUser resultUser = new ResultUser();
		resultUser.setObode(resume.getObode());
		resultUser.setBirthday(resume.getBirthday());
		resultUser.setTrueName(user.getTrueName());
		resultUser.setSex(user.getSex());
		resultUser.setEmail(user.getEmail());
		resultUser.setProfileUrl(user.getProfileUrl());
		resultUser.setTelephone(user.getTelphone());
		if(user.getAccountType()==Resume.STUDENT_ACCOUNT_TYPE){
			resultUser.setIsStudent(Resume.IS_STUDENT);
		}else{
			resultUser.setIsStudent(Resume.NOT_STUDENT);
		}
	 	return OAuthResult.ok(resultUser);
    }
    
    /**
     * 验证access_token是否有效
     * @param accessToken
     * @return
     */
    public OAuthResult checkAccessToken(String accessToken) {
    	if(clientUserDao.findByAccessToken(accessToken) != null){
        	return OAuthResult.ok();
        }
        return OAuthResult.build(OAuth.StatusCode.NOT_FOUND, "没有对应的accessToken");
    }
    
    /**
     * 添加access_token
     * @param client
     */
    public OAuthResult addAccessToken(String authorizationCode, String accessToken) {
    	ClientUser clientUser = clientUserDao.findByAuthorizationCode(authorizationCode);
        clientUser.setAccessToken(accessToken);
        
        Date date = new Date();
        SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMddHHmmssSSS");
        clientUser.setCreateTime(Long.parseLong(sdf.format(date)));
        
        clientUserDao.save(clientUser);
        return OAuthResult.ok();
    }
    
    /**
     * 添加access_token
     * @param client
     */
    public OAuthResult addAuthCode(String accountId, String serviceId, String authorizationCode) {
    	ClientUser clientUser = new ClientUser();
    	clientUser.setAccountId(Long.parseLong(accountId));
    	clientUser.setServiceId(Long.parseLong(serviceId));
        clientUser.setAuthorizationCode(authorizationCode);
        
        clientUserDao.save(clientUser);
        return OAuthResult.ok();
    }
    
    /**
     * 验证auth_code是否有效
     * @param authCode
     * @return
     */
    public OAuthResult checkAuthCode(String authCode) {
        if(clientUserDao.findByAuthorizationCode(authCode) != null){
        	return OAuthResult.ok();
        }
        return OAuthResult.build(OAuth.StatusCode.NOT_FOUND, "没有对应的authCode");
    }
    
    

}
